﻿using System.Linq;

namespace Basmus.Conel
{
    public class NullableBinding<TElement> : Binding
        where TElement : struct
    {
        public NullableBinding()
            : base(typeof(TElement))
        {

        }

        public override object[] GetElements(object container)
        {
            var nullable = (TElement?) container;
            var elements = nullable.HasValue
                               ? new[] {nullable.Value}.Cast<object>().ToArray()
                               : new object[0];
            return elements;
        }

        public override object GetContainer(object[] elements)
        {
            if (elements == null || elements.Length == 0 || elements[0] == null)
                return null;
            TElement? container = (TElement) elements[0];
            return container;
        }
    }
}
